package com.podmanager.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

/*
 * Yes, the playlist is a singleton object since there is 
 * only one playlist.
 * 
 * I must have went crazy.  Who the hell uses singleton objects? just kidding
 */
public class Playlist {

	private static Playlist playlist;
	private List<String> sortedPlayList;
	// private Map<String, String> settings;
	private String currentItem;
	
	
	private Playlist() {
		sortedPlayList = new ArrayList<String>();
	}
	
	public static Playlist getPlaylist() {
		if (playlist == null) 
			playlist = new Playlist();
		return playlist;	
	}
	
	/*
	 * Gets the current item from the Settings table.
	 */
	public String queryCurrentItem(ContentResolver contentResolver) {
		Cursor c = null;
		try {
			c = contentResolver.query(DBContentProvider.SETTINGS_URI, 
					new String[] {PMDatabase.SETTINGS.VALUE_ROW}, 
					PMDatabase.SETTINGS.KEY_ROW + "=" + "'FILE'", null, null);
			c.moveToFirst();
			this.currentItem = c.getString(0);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			c.close();
		}
		
		return this.currentItem;
	}
	
	/*
	 * Change the current item in the database.
	 * 
	 * Used when getting the next item
	 */
	public int updateCurrentItem(ContentResolver contentResolver, String newItem) {
		int rows=0;
		try {
			ContentValues values = new ContentValues();
			this.currentItem = queryCurrentItem(contentResolver);
			values.put(PMDatabase.SETTINGS.VALUE_ROW, newItem);
			rows = contentResolver.update(DBContentProvider.SETTINGS_URI, values, 
											PMDatabase.SETTINGS.VALUE_ROW + "=?", 
											new String[] {this.currentItem} );
			this.currentItem = newItem;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rows;
	}

	public String getCurrentItem() {
		return currentItem;
	}

	public void setCurrentItem(String currentItem) {
		this.currentItem = currentItem;
	}
	
	/*
	 * Gets the next file in the playlist.  This also iterates
	 * the current file to the next file.
	 */
	public String getNextItem() {
		String nextItem = null;
		if (sortedPlayList.contains(this.currentItem)) {
			int element = sortedPlayList.indexOf(this.currentItem);
			try {
			nextItem = sortedPlayList.get(element+1);
			this.currentItem = nextItem;
			} catch (Exception e) {
				// the playlist goes back to the first file when it hits the last file
				this.currentItem = sortedPlayList.get(0);
			}
			
		}
		return this.currentItem;
	}
	
	public boolean isPlaylistEmpty() {
		return (sortedPlayList.size() <= 0);
	}
	
	public String getFirstItem() {
		String returnValue = null;
		if (sortedPlayList != null) 
			if (sortedPlayList.size() > 0) {
				this.currentItem = sortedPlayList.get(0); 
				returnValue = this.currentItem;
			}
			//returnValue = (sortedPlayList.get(0) != null) ? this.currentItem = sortedPlayList.get(0) : null;

		return returnValue;
	}

	public List<String> getSortedPlayList() {
		return sortedPlayList;
	}

	public void setSortedPlayList(List<String> sortedPlayList) {
		this.sortedPlayList = sortedPlayList;
	}
	
	/*
	 * Call this method to update the playlist singleton.
	 */
	public void updatePlaylist(ContentResolver contentResolver, String SORT) {
		Playlist.getPlaylist().getSortedPlayList().clear();
		Playlist playlist = Playlist.getPlaylist();
		retrievePlaylistItems(contentResolver, SORT, playlist);
	}

	private void retrievePlaylistItems(ContentResolver contentResolver,
			String SORT, Playlist playlist) {
		Cursor c = contentResolver.query(DBContentProvider.PLAYLIST_URI, 
				new String[] { PMDatabase.EPISODES.FILE_PATH}, 
				PMDatabase.EPISODES.IS_IN_PLAYLIST + "=" + 1, 
				null, SORT); 
		
		while(c.moveToNext()) 
			playlist.getSortedPlayList().add(c.getString(0));

		c.close();
	}
 
	public void insertSettings(Map<String, String> settings, ContentResolver contentResolver) {
		
		ContentValues values = new ContentValues();
		Iterator iterator = settings.entrySet().iterator();
		
		int rowsAffected = contentResolver.delete(DBContentProvider.SETTINGS_URI, null, null);
				
		while (iterator.hasNext()) {
			Map.Entry entry = (Map.Entry<String, String>) iterator.next();
			values = new ContentValues();
			values.put(PMDatabase.SETTINGS.KEY_ROW, (String) entry.getKey());
			values.put(PMDatabase.SETTINGS.VALUE_ROW, (String) entry.getValue());
			Uri uri = contentResolver.insert(DBContentProvider.SETTINGS_URI, values);
		}
		currentItem = settings.get("FILE");
		
		//c.close();
	}
	
}







